package com.jshan.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshan.base.entity.SysUser;
import com.jshan.base.vo.Result;
import com.jshan.common.annotation.OperationLog;
import com.jshan.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class SysUserController {
    private final SysUserService userService;

    @Operation(summary = "获取用户信息以及权限信息")
    @GetMapping("/info")
    @OperationLog("获取用户信息以及权限信息")
    public Result<Map<String, Object>> getInfo() {
        Long userId = StpUtil.getLoginIdAsLong();
        SysUser user = userService.getById(userId);
        List<String> permissions = StpUtil.getPermissionList();

        Map<String, Object> data = new HashMap<>();
        data.put("user", user);
        data.put("permissions", permissions);
        return Result.ok(data);
    }

    @Operation(summary = "用户列表")
    @SaCheckPermission("system:user:list")
    @GetMapping("/list")
    public Result<List<SysUser>> list() {
        return Result.ok(200, "成功", userService.listAll());
    }

    @Operation(summary = "用户分页")
    @SaCheckPermission("system:user:list")
    @OperationLog("查询用户列表")
    @GetMapping("/page")
    public Result<IPage<SysUser>> getUserPage(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<SysUser> page = new Page<>(pageNum, pageSize);
        IPage<SysUser> userPage = userService.getUserPage(page);
        return Result.ok(userPage);
    }

    @SaCheckPermission("system:user:list")
    @OperationLog("根据部门ID查询用户列表")
    @GetMapping("/pageByDept")
    public Result<IPage<SysUser>> getUsersByDeptId(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long deptId,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) Integer status) {
        IPage<SysUser> page = new Page<>(pageNum, pageSize);
        IPage<SysUser> userPage = userService.getUsersByDeptId(page, deptId, username, phone, status);
        return Result.ok(userPage);
    }

    @SaCheckPermission("system:user:add")
    @OperationLog("新增用户")
    @PostMapping
    public Result<Void> addUser(@RequestBody SysUser user) {
        userService.saveUser(user);
        return Result.ok();
    }

    @SaCheckPermission("system:user:edit")
    @OperationLog("修改用户")
    @PutMapping
    public Result<Void> updateUser(@RequestBody SysUser user) {
        userService.updateUser(user);
        return Result.ok();
    }

    @SaCheckPermission("system:user:delete")
    @OperationLog("删除用户")
    @DeleteMapping("/{id}")
    public Result<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.ok();
    }

    @SaCheckLogin // 登录后即可访问
    @OperationLog("更新个人信息")
    @PutMapping("/profile")
    public Result<Void> updateProfile(@RequestBody SysUser user) {
        user.setId(StpUtil.getLoginIdAsLong()); // 确保更新的是当前登录用户
        if (userService.updateProfile(user)) {
            return Result.ok("个人信息更新成功");
        } else {
            return Result.fail(500, "个人信息更新失败");
        }
    }

    @SaCheckLogin // 登录后即可访问
    @PutMapping("/password")
    @OperationLog("修改密码")
    public Result<Void> updatePassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        Long userId = StpUtil.getLoginIdAsLong();
        if (userService.updatePassword(userId, oldPassword, newPassword)) {
            return Result.ok(200, "密码修改成功");
        } else {
            return Result.fail(500, "旧密码不正确或密码修改失败");
        }
    }

    @SaCheckLogin // 登录后即可访问
    @PostMapping("/avatar")
    @OperationLog("上传头像")
    public Result<Map<String, String>> uploadAvatar(@RequestParam("file") MultipartFile file) {
        Long userId = StpUtil.getLoginIdAsLong();
        String avatarUrl = userService.uploadAvatar(userId, file);
        if (avatarUrl != null) {
            Map<String, String> data = new HashMap<>();
            data.put("avatarUrl", avatarUrl);
            return Result.ok(data);
        } else {
            return Result.fail(500, "头像上传失败");
        }
    }
}
